
/*****************************************************************************
* variance decomposition of financial distress measures (Table 2)
*****************************************************************************/


* load cz level csv file from spark
* append to create cz level covariates across all year
clear

forval y= 0/16{

	preserve
		if `y'<10{
			import delimited using "${dataRoot}/sumstats_200`y'09_${Geo}.csv", ///
			delimiters(",") varnames(1) clear
		}
		else{
			import delimited using "${dataRoot}/sumstats_20`y'09_${Geo}.csv", ///
			delimiters(",") varnames(1) clear			
		}
		*save
		save  "${dataRoot}/sumstats_pool_${Geo}", replace
	
	restore
	append using "${dataRoot}/sumstats_pool_${Geo}"
	

}
drop if cz==.
save  "${dataRoot}/sumstats_pool_${Geo}_09_all_years", replace


* load aggregated data across all years
use "${dataRoot}/sumstats_pool_${Geo}_09_all_years", clear

foreach var in $keyMvVarsShow {
	sum `var'
	gen bar_it_`var' = `r(mean)'

	bysort cz: egen bar_t_`var' = mean(`var')
	
	* total, within, between variation
	gen `var'_total = (`var' - bar_it_`var')^2
	gen `var'_within = (`var' - bar_t_`var')^2
	gen `var'_between = (bar_t_`var' - bar_it_`var')^2
}

gen n=1
collapse (sum) *_total *_within *_between, by(n)

* percentage of total variation
foreach var in $keyMvVarsShow {

	replace `var'_within  = 100*`var'_within /`var'_total 
	replace `var'_between = 100*`var'_between/`var'_total 
	
}

drop *_total


reshape long $keyMvVarsShow , i(n) string

* create latex table
estpost sum  $keyMvVarsShow if _j=="_within" 
est store within
estpost sum  $keyMvVarsShow if _j=="_between" 
est store across


labelVars 1 1 "\addlinespace[0.1em] \\ "
esttab within across  using "$outDir/Tex/sumstats_within_vs_across_short.tex", replace ///
	cells("mean(fmt(%9.2fc))") ///
	mtitle("\shortstack{Time-series}" "\shortstack{Cross-section}") ///
	nonumber noobs collabels(none)  label
	